﻿using Cms.Application.Admin.Presenters;
using Cms.Application.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cms.Domain;
using Cms.Application.Admin.Views;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class EditApplicationPagePresenterTests : BasePresenterTests
    {
        private Site _site;
        private Locale _localeEn;
        private Locale _localeSe;
        private Administrator _admin;
        private Mock<IEditApplicationPageView> _viewMock;
        
        private void Prepare()
        {
            InitRepositoryFactoryMock();
            _admin = new Administrator();
            _site = new Site { Domain = "www.fredium.com" };
            _admin.CurrentSite = _site;
            MasterTemplate mt = new MasterTemplate { ID = 1 };
            _site.MasterTemplates.Add(mt);
            _site.DefaultMasterTemplate = mt;
            DetailTemplate dt = new DetailTemplate { ID = 1 };
            _site.DetailTemplates.Add(dt);
            _site.DefaultDetailTemplate = dt;
            dt.PageAreas.Add("pcMain");
            _localeEn = new Locale { ID = 1, Site = _site };
            _localeSe = new Locale { ID = 2, Site = _site };
            _site.Locales.Add(_localeEn);
            _site.Locales.Add(_localeSe);
            _site.DefaultLocale = _localeEn;
            AdministratorRepositoryMock.Setup(a => a.GetAdministrator(1)).Returns(_admin);
            _viewMock = new Mock<IEditApplicationPageView>();
            _viewMock.SetupAllProperties();
            _viewMock.Setup(v => v.AdministratorID).Returns(1);
            _viewMock.Setup(v => v.Localization).Returns("en-US");
            _viewMock.Setup(v => v.ContextTranslationFile).Returns("Admin/Translations/EditPage.xml");
            SiteRepositoryMock.Setup(m => m.GetMasterTemplate(1)).Returns(mt);
            SiteRepositoryMock.Setup(m => m.GetDetailTemplate(1)).Returns(dt);
        }
        
        [TestMethod]
        public void Initialize_NewApplicationPage_AdministratorHasAlreadySelectedAFolder_SelectedFolderIsMinusOne()
        {
            Prepare();
            _admin.SelectedFolderID = 3;
            _viewMock.Setup(v => v.PageID).Returns(-1);
            EditApplicationPagePresenter controller = new EditApplicationPagePresenter(_viewMock.Object, FactoryMock.Object);
            Mock<IDiskAccess> io = new Mock<IDiskAccess>();
            controller.DiskAccess = io.Object;
            controller.Initialize();
            Assert.AreEqual(-1, _viewMock.Object.SelectedFolderID);
        }
    }
}
